package com.bw.kotlindemo.java.model.net;

import android.text.TextUtils;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bw.kotlindemo.java.model.bean.CloseLiveBean;
import com.bw.kotlindemo.java.model.bean.LiveBean;
import com.bw.kotlindemo.java.model.bean.LiveListBean;
import com.bw.kotlindemo.java.model.bean.LoginBean;
import com.bw.kotlindemo.java.model.bean.LoginResult;
import com.bw.kotlindemo.java.model.bean.TypeBean;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ApiModel {

    static {
        //刘鹏欣 测试机
//        String uid = "76";
//        String sessionId = "167834643125776";


        //lwt 测试机
        String uid = "77";
        String sessionId = "167842756499477";

        //赵恒威 测试机
        uid = "79";
        sessionId = " 167886914554879";

        //张拯源 测试机
        //        String uid = "78";
        //        String sessionId = "167843047566478";

//        SPUtils.getInstance().put("uid", uid);
//        SPUtils.getInstance().put("sessionId", sessionId);


    }

    private static String uid = "";
    private static String sessionId = "";

    public static String getSessionId() {
        if (TextUtils.isEmpty(sessionId)) {
            sessionId = SPUtils.getInstance().getString("sessionId");
        }
        return sessionId == null ? "" : sessionId;
    }

    public static String getUid() {
        if (TextUtils.isEmpty(uid)) {
            uid = SPUtils.getInstance().getString("uid");
        }
        return uid == null ? "" : uid;
    }

    public static void doLogin(String phone, String pwd, Consumer<LoginBean> callback) {
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("pwd", pwd);
        RetrofitManager.getInstance()
                .getApiService()
                .doLogin(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }

    public static void doLoginLive(String nickNmae, String userNmae,String passwd, Consumer<LoginResult> callback) {
        Map map = new HashMap();
//        map.put("nickName", nickNmae);
//        map.put("userName", userNmae);
//        map.put("passwd", passwd);

        map.put("nickName", "xsd01");
        map.put("userName", "saild02");
        map.put("passwd", "a12345678");
        RetrofitManager.getInstance()
                .getApiService()
                .doLogin24(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }

    public static void getTypeList(Consumer<TypeBean> callback) {
        RetrofitManager.getInstance()
                .getApiService()
                .getListType()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }

    /**
     * 获取直播列表
     */
    public static void getLiveListByType(int liveId, int page, int size, Consumer<LiveListBean> callback) {
        RetrofitManager.getInstance()
                .getApiService().getLiveByType(liveId, page, size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }

    public static void openLive(int liveType, String liveTitle, String filePath, Consumer<LiveBean> callback) {
        File file = FileUtils.getFileByPath(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("liveCoverImage", file.getName(), requestBody);
        RetrofitManager.getInstance()
                .getApiService().openLive(liveType, liveTitle, "111", "12", part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }

    public static void closeLive(Consumer<CloseLiveBean> callback) {
        RetrofitManager.getInstance()
                .getApiService().closeLive()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback);
    }
}
